import request from '@/utils/request';

export const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip',
};

export const fullMap = [
  { type: 'doc', application: 'application/msword' },
  {
    type: 'docx',
    application: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  },
  { type: 'dot', application: 'application/msword' },
  {
    type: 'dotx',
    application: 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  },
  { type: 'xls', application: 'application/vnd.ms-excel' },
  {
    type: 'xlsx',
    application: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  },
  { type: 'ppt', application: 'application/vnd.ms-powerpoint' },
  {
    type: 'pptx',
    application: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  },
  { type: 'pdf', application: 'application/pdf' },
  { type: 'txt', application: 'text/plain' },
  { type: 'gif', application: 'image/gif' },
  { type: 'jpeg', application: 'image/jpeg' },
  { type: 'jpg', application: 'image/jpeg' },
  { type: 'png', application: 'image/png' },
  { type: 'css', application: 'text/css' },
  { type: 'html', application: 'text/html' },
  { type: 'htm', application: 'text/html' },
  { type: 'xsl', application: 'text/xml' },
  { type: 'xml', application: 'text/xml' },
  { type: 'mpeg', application: 'video/mpeg' },
  { type: 'mpg', application: 'video/mpeg' },
  { type: 'avi', application: 'video/x-msvideo' },
  { type: 'movie', application: 'video/x-sgi-movie' },
  { type: 'bin', application: 'application/octet-stream' },
  { type: 'exe', application: 'application/octet-stream' },
  { type: 'so', application: 'application/octet-stream' },
  { type: 'dll', application: 'application/octet-stream' },
  { type: 'ai', application: 'application/postscript' },
  { type: 'dir', application: 'application/x-director' },
  { type: 'js', application: 'application/x-javascript' },
  { type: 'swf', application: 'application/x-shockwave-flash' },
  { type: 'xhtml', application: 'application/xhtml+xml' },
  { type: 'xht', application: 'application/xhtml+xml' },
  { type: 'zip', application: 'application/zip' },
  { type: 'mid', application: 'audio/midi' },
  { type: 'midi', application: 'audio/midi' },
  { type: 'mp3', application: 'audio/mpeg' },
  { type: 'rm', application: 'audio/x-pn-realaudio' },
  { type: 'rpm', application: 'audio/x-pn-realaudio-plugin' },
  { type: 'wav', application: 'audio/x-wav' },
  { type: 'bmp', application: 'image/bmp' },
];

export const allowMap = [
  { type: 'doc', application: 'application/msword' },
  {
    type: 'docx',
    application: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  },
  { type: 'xls', application: 'application/vnd.ms-excel' },
  {
    type: 'xlsx',
    application: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  },
  { type: 'ppt', application: 'application/vnd.ms-powerpoint' },
  {
    type: 'pptx',
    application: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  },
  { type: 'pdf', application: 'application/pdf' },
  { type: 'txt', application: 'text/plain' },
  { type: 'gif', application: 'image/gif' },
  { type: 'jpeg', application: 'image/jpeg' },
  { type: 'jpg', application: 'image/jpeg' },
  { type: 'png', application: 'image/png' },
  { type: 'bmp', application: 'image/bmp' },
];

export function downLoadZip(url: string) {
  request(url, {
    method: 'GET',
    responseType: 'blob',
    getResponse: true,
  }).then((res) => {
    resolveBlob(res, mimeMap.zip);
  });
}

/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob(res: any, mimeType: string) {
  const aLink = document.createElement('a');
  const blob = new Blob([res.data], { type: mimeType });
  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
  const contentDisposition = decodeURI(res.response.headers.get('content-disposition'));
  const result = patt.exec(contentDisposition);
  let fileName = result ? result[1] : 'file';
  fileName = fileName.replace(/"/g, '');
  aLink.style.display = 'none';
  aLink.href = URL.createObjectURL(blob);
  aLink.setAttribute('download', fileName); // 设置下载文件名称
  document.body.appendChild(aLink);
  aLink.click();
  URL.revokeObjectURL(aLink.href); // 清除引用
  document.body.removeChild(aLink);
}


export async function downLoadXlsx(url: string, params: any, fileName: string) {
  return request(url, {
    ...params,
    method: 'POST',
    responseType: 'blob',
  }).then((data) => {
    const aLink = document.createElement('a');
    const blob = data; // new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    aLink.style.display = 'none';
    // @ts-ignore
    aLink.href = URL.createObjectURL(blob);
    aLink.setAttribute('download', fileName); // 设置下载文件名称
    document.body.appendChild(aLink);
    aLink.click();
    URL.revokeObjectURL(aLink.href); // 清除引用
    document.body.removeChild(aLink);
  });
}

/**
 * @param arr 查找数组
 * @param property 查找属性
 * @param value 查找值
 * @param findKey
 * @des:查找到值后返回其值
 */
export const findArrayReturnValue = (
  arr: string | any[],
  property: string,
  value: any,
  findKey: string,
) => {
  let findValue;
  if (!Array.isArray(arr)) {
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    if (
      arr[i].hasOwnProperty(property) &&
      arr[i][property] == value &&
      arr[i].hasOwnProperty(findKey)
    ) {
      findValue = arr[i][findKey];
      break;
    }
  }
  return findValue;
};

/**
 * [getFileType 下载文档对应的类型]
 */
export function getFileApplication(id: any) {
  return findArrayReturnValue(fullMap, 'type', id, 'application');
}

/**
 * [getFileType 下载文档对应的类型]
 */
export function getAllowedFileType(id: any) {
  return findArrayReturnValue(allowMap, 'application', id, 'type');
}
